import React, { Component } from 'react'
import './index.css'

export default class Item extends Component {
    state = {
        isMouseHover:false
    }
    handleMouse = (flag) => {
        return () => {
            this.setState({isMouseHover:flag})
        }
    }
    handleCheck = (id) => {
        const {updateTodo} = this.props
        return (event) => {
            // console.log(id,event.target.checked)
            updateTodo(id,event.target.checked)
        }
    }
    handleClick = (id) => {
        const {deleteTodo} = this.props
        // 小坑，前面要加window
        return (event) => {
            if(window.confirm('确定删除?')){
                deleteTodo(id)
            }
        }
    }
    render() {
        const {id,name,done} = this.props
        const {isMouseHover} = this.state
        return (
            <li style={{backgroundColor:isMouseHover ? '#ddd' : 'white'}} onMouseEnter={this.handleMouse(true)} onMouseLeave={this.handleMouse(false)}>
                <label>
                    <input type="checkbox" checked={done} onChange={this.handleCheck(id)} />
                    <span>{name}</span>
                </label>
                <button onClick={this.handleClick(id)} className="btn btn-danger" style={{display:isMouseHover ? 'block' : 'none'}}>删除</button>
            </li>
        )
    }
}
